package registnet.records.source;

import java.util.List;

import registnet.dao.GradePeer;
import registnet.records.CourseTaken;
import registnet.records.Form5;
import registnet.records.Section;
import registnet.records.Student;
import resources.Constants;

public class Form5Source {

	private Student student;
	private int semester;
	private int year;
	private Form5 form5;
	
	
	public Form5Source(Student student, int semester, int year){
		this.student = student;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	public void load(){
		try{
			registnet.dao.Student DAOStudent = new registnet.dao.StudentPeer().getStudent(this.student.getStudentNumber());
			List<registnet.dao.Grade> DAOGrades = new registnet.dao.GradePeer().getGradeByStudent(DAOStudent.getStudentId(), this.semester, this.year);
			this.form5 = new Form5(this.student, this.semester, this.year);
			for(int i=0; i<DAOGrades.size(); i++){
				registnet.dao.Grade DAOGrade = DAOGrades.get(i);
				registnet.dao.Course DAOCourse = DAOGrade.getCourse();
				CourseTaken ct = new CourseTaken(this.student, DAOCourse.getCourseCode(), this.semester, this.year);
				ct.setCourseName(DAOCourse.getCourseName());
				ct.setCredit(DAOGrade.getCredit());
				ct.setGrade(DAOGrade.getGrade());
				ct.setUnit(DAOCourse.getUnits());
				ct.setSection(new Section(DAOGrade.getSectionName()));
				ct.setRemarks(DAOGrade.getRemarks());
				if (!DAOGrade.getSectionName().equals(Constants.REMOVAL) && !DAOGrade.getSectionName().equals(Constants.COMPLETION)){
					this.form5.addCourse(ct);
					if (DAOGrade.getGrade() == Constants.MAP_GRADE_KEY.get("4")){
						((CourseTaken)this.form5.getCourses().get(DAOCourse.getCourseCode())).setReExam(new GradePeer().getRemovalGrade(DAOGrade));
					}					
					else if (DAOGrade.getGrade() == Constants.MAP_GRADE_KEY.get("INC.")){
						((CourseTaken)this.form5.getCourses().get(DAOCourse.getCourseCode())).setReExam(new GradePeer().getCompletionGrade(DAOGrade));
					}	
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Form5 getForm5(){
		return this.form5;
	}
	
}
